<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\shop\controller;

class IndexWxPay extends \app\ApiRest
{
    protected $app;
    public function __construct(\think\App $app)
    {
        $this->app = $app;
    }
    public function createWeixinPay($paymentApp, $openid, $uniacid, $body, $attach, $totalprice)
    {
        $adapay_config_model = new \app\adapay\model\Config();
        $adapay_config = $adapay_config_model->dataInfo(["uniacid" => $uniacid]);
        if($adapay_config["status"] == 1) {
            $adapay = new \longbingcore\wxcore\Adapay($uniacid);
            $adapay_code = orderCode();
            $res = $adapay->createPay($adapay_code, $totalprice, $openid);
            if($res["status"] == "succeeded" && isset($res["expend"]["pay_info"])) {
                $pay_record = new \app\adapay\model\PayRecord();
                $dis = ["uniacid" => $uniacid, "order_code" => $attach["out_trade_no"]];
                $pay_record->dataUpdate($dis, ["status" => -1]);
                $insert = ["uniacid" => $uniacid, "order_code" => $attach["out_trade_no"], "adapay_code" => $adapay_code, "res_id" => $res["id"]];
                $pay_record->dataAdd($insert);
                return json_decode($res["expend"]["pay_info"], true);
            }
        }
        global $_GPC;
        global $_W;
        $setting["mini_appid"] = $paymentApp["app_id"];
        $setting["mini_appsecrept"] = $paymentApp["secret"];
        $setting["mini_mid"] = $paymentApp["payment"]["merchant_id"];
        $setting["mini_apicode"] = $paymentApp["payment"]["key"];
        $setting["apiclient_cert"] = $paymentApp["payment"]["cert_path"];
        $setting["apiclient_cert_key"] = $paymentApp["payment"]["key_path"];
        define("WX_APPID", $setting["mini_appid"]);
        define("WX_MCHID", $setting["mini_mid"]);
        define("WX_KEY", $setting["mini_apicode"]);
        define("WX_APPSECRET", $setting["mini_appsecrept"]);
        define("WX_SSLCERT_PATH", $setting["apiclient_cert"]);
        define("WX_SSLKEY_PATH", $setting["apiclient_cert_key"]);
        define("WX_CURL_PROXY_HOST", "0.0.0.0");
        define("WX_CURL_PROXY_PORT", 0);
        define("WX_REPORT_LEVENL", 0);
        require_once PAY_PATH . "/weixinpay/lib/WxPay.Api.php";
        require_once PAY_PATH . "/weixinpay/example/WxPay.JsApiPay.php";
        $tools = new \JsApiPay();
        $input = new \WxPayUnifiedOrder();
        $input->SetBody($body);
        $input->SetAttach(json_encode($attach));
        $input->SetOut_trade_no($attach["out_trade_no"]);
        $input->SetTotal_fee($totalprice * 100);
        $input->SetTime_start(date("YmdHis"));
        $param_arr = ["i" => $uniacid, "t" => $_GPC["t"], "v" => $_GPC["v"], "n" => APP_MODEL_NAME];
        $reply_path = json_encode($param_arr);
        if(defined("IS_WEIQIN")) {
            $path = "https://" . $_SERVER["HTTP_HOST"] . "/addons/" . APP_MODEL_NAME . "/core2/app/Common/wexinPay.php?params=" . $reply_path;
            $paths = "https://" . $_SERVER["HTTP_HOST"] . "/addons/" . APP_MODEL_NAME . "/core2/app/Common/wexinPay.php?ck=789";
            $a = @file_get_contents($paths);
            if($a != 1) {
                $this->errorMsg("发起支付失败");
            }
        } else {
            $path = "https://" . $_SERVER["HTTP_HOST"] . "/wexinPay.php?params=" . $reply_path;
            $paths = "https://" . $_SERVER["HTTP_HOST"] . "/wexinPay.php?ck=789";
            $a = @file_get_contents($paths);
            if($a != 1) {
                $this->errorMsg("发起支付失败");
            }
        }
        $this->lb_logOutput("BaseApiPath:-----" . $path);
        $input->SetNotify_url($path);
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openid);
        $order = \WxPayApi::unifiedOrder($input);
        if(!empty($order["return_code"]) && $order["return_code"] == "FAIL") {
            $this->errorMsg($order["return_msg"]);
        }
        $jsApiParameters = $tools->GetJsApiParameters($order);
        $jsApiParameters = json_decode($jsApiParameters, true);
        if(!empty($jsApiParameters["return_code"])) {
            $this->errorMsg("发起支付失败");
        }
        return $jsApiParameters;
    }
    public function lb_logOutput($data, $flag = 0)
    {
        if($flag == 0) {
            return NULL;
        }
        if(is_array($data)) {
            $data = json_encode($data);
        }
        $filename = "./" . date("Y-m-d") . ".log";
        $str = date("Y-m-d H:i:s") . "   " . $data . "\r\n";
        file_put_contents($filename, $str, FILE_APPEND | LOCK_EX);
    }
    public function returnPay()
    {
        $this->lb_logOutput("in--mingpianNotify");
        $xmlData = file_get_contents("php://input");
        if(empty($xmlData)) {
            $xmlData = "empty   xmlData";
        }
        $this->lb_logOutput("xmlData in mingpian:-----" . $xmlData);
        $this->lb_logOutput("in-mingpian2");
        global $_GPC;
        $xmlData = file_get_contents("php://input");
        $this->lb_logOutput("in-mingpian-\$xmlData:-----" . $xmlData);
        if(defined("IS_WEIQIN")) {
            $uniacid = $_GPC["i"];
        } else {
            $uniacid = $_GET["i"];
        }
        $paymentApp = $this->payConfig($uniacid);
        $this->lb_logOutput("in-mingpian-uniacid:-----" . $uniacid);
        $setting["mini_appid"] = $paymentApp["app_id"];
        $setting["mini_appsecrept"] = $paymentApp["secret"];
        $setting["mini_mid"] = $paymentApp["payment"]["merchant_id"];
        $setting["mini_apicode"] = $paymentApp["payment"]["key"];
        $setting["apiclient_cert"] = $paymentApp["payment"]["cert_path"];
        $setting["apiclient_cert_key"] = $paymentApp["payment"]["key_path"];
        define("WX_APPID", $setting["mini_appid"]);
        define("WX_MCHID", $setting["mini_mid"]);
        define("WX_KEY", $setting["mini_apicode"]);
        define("WX_APPSECRET", $setting["mini_appsecrept"]);
        define("WX_SSLCERT_PATH", $setting["apiclient_cert"]);
        define("WX_SSLKEY_PATH", $setting["apiclient_cert_key"]);
        define("WX_CURL_PROXY_HOST", "0.0.0.0");
        define("WX_CURL_PROXY_PORT", 0);
        define("WX_REPORT_LEVENL", 0);
        require_once PAY_PATH . "/weixinpay/lib/WxOrderNotify.php";
        $WxPay = new \WxOrderNotify($this->app);
        $WxPay->Handle(false);
    }
}

